package com.fjnu.controller;

import com.fjnu.dto.UserConditionDto;
import com.fjnu.dto.UserDto;
import com.fjnu.uitl.ResponseResult;
import com.fjnu.entity.User;
import com.fjnu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("list")
    public ResponseResult queryUsers(UserConditionDto userConditionDto){
        List<User> list = userService.list(userConditionDto);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode("200");
        responseResult.setMessage("查询成功");
        responseResult.setData(list);
        return responseResult;
    }
    @PostMapping("add")
    public ResponseResult add(UserDto userDto){
        ResponseResult responseResult = new ResponseResult();
        int add = userService.add(userDto);
        if (add > 0){
            responseResult.setCode("200");
            responseResult.setMessage("注册成功");
        }
        else{
            responseResult.setCode("500");
            responseResult.setMessage("注册失败");
        }
        return responseResult;
    }
    @PostMapping("password/change")
    public ResponseResult changePassword(Integer id, String password){
        int i = userService.updatePassword(id, password);
        ResponseResult responseResult = new ResponseResult();
        if (i > 0){
            responseResult.setCode("200");
            responseResult.setMessage("修改密码成功");
        }
        else{
            responseResult.setCode("500");
            responseResult.setMessage("修改密码失败");
        }
        return responseResult;
    }
}
